package com.top.vueservice.controller;

import com.top.vueservice.entities.Params;
import com.top.vueservice.entities.Product;
import com.top.vueservice.entities.User;
import com.top.vueservice.service.ProductService;
import com.top.vueservice.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@CrossOrigin
public class ProductController {
    @Resource
    private ProductService productService;

    @Resource
    private UserService userService;

    @GetMapping("/getProById/{lId}")
    @SuppressWarnings("unchecked")
    public CommonResult getById(@PathVariable("lId") long lId){
        log.info("请求成功 lId="+lId);

        return new CommonResult(200,"获取成功",productService.getProById(lId));
    }
    @GetMapping("/getAll")
    public CommonResult getAll(){
        return new CommonResult(200,"获取成功",productService.getAll());
    }
    @PostMapping("/add")
    public Integer add(@RequestBody Product product){
        log.info(product.getStrProductName());
        return productService.addPro(product);
    }

    @RequestMapping("/velidataPwd")
    public Integer velidataPwd(String strUserName,String strPassWord){
        log.info(strUserName+"-"+strPassWord);
        User user=new User(strUserName,strPassWord);
        return  userService.velidataPwd(user);

    }

    @RequestMapping("/login")
    public Integer velidataPwd2(@RequestBody User user){
        log.info(user.getStrUserName());
        return  userService.velidataPwd(user);

    }
    @RequestMapping("/getProByPage")
    public CommonResult getProByPage(@RequestBody Params params){
        log.info("getProByPage--");
        Integer totalCount = productService.getTotalCount(params);
        List<Product> proList = productService.getAllByPage(params);
        return  new CommonResult(totalCount,"总数",proList);
    }
    @PostMapping("/updatePro")
    public CommonResult update(@RequestBody Product product){
        log.info(product.getStrProductName());
        Integer integer = productService.updateProById(product);
        return new CommonResult(200,"添加成功",integer);
    }
    @PostMapping("/delPro")
    public CommonResult del(@RequestBody Product product){
        log.info("del:"+product.getLId());
        Integer integer = productService.delProById(product);
        return new CommonResult(200,"删除成功",integer);

    }



}
